﻿using ChineseSchool.BLL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ChineseSchool.Admin
{
    public partial class ucChangePassword : System.Web.UI.UserControl
    {
        private B_User userService = new B_User();
        private B_Insert_Update_Delete insertUpdate = new B_Insert_Update_Delete();

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        private bool CheckOldPass()
        {
            if (txtOldPass.Text.Trim().Equals(Session["PassWord"].ToString()))
                return true;
            return false;
        }

        protected void BtnUpdateClicked(object sender, EventArgs e)
        {
            string userName = Session["Username"].ToString();
            string userPassword = Session["PassWord"].ToString();
            var currentUser = userService.Selectby_username_password(userName, userPassword);

            if (!string.IsNullOrEmpty(txtOldPass.Text.Trim()))
            {
                if (!CheckOldPass())
                {
                    Response.Write("<script type='text/javascript'>alert('Mật khẩu cũ không đúng!')</script>");
                    return;
                }
            }

            if (!string.IsNullOrEmpty(txtPassword.Text.Trim()) || !string.IsNullOrEmpty(txtRePass.Text.Trim()))
            {
                if (txtPassword.Text.Trim().Equals(txtRePass.Text.Trim()))
                    currentUser.Password = txtRePass.Text;
                else
                {
                    Response.Write("<script type='text/javascript'>alert('Mật khẩu mới không trùng nhau!')</script>");
                    return;
                }
            }
            insertUpdate.update("Users", "UserID", currentUser, currentUser.UserID);
            Response.Write("<script type='text/javascript'>alert('Cập nhập thành công')</script>");
        }
    }
}